<%@ page contentType="text/html; charset=utf-8" language="java" extends="org.zizi.service.JspPageServlet" %>
<%@page import="org.zizi.manager.impl.DaoManager"%>
<%@page import="org.zizi.manager.Managers"%>
<%@page import="org.zizi.dao.VatTypeDao"%>
<%@page import="org.zizi.modal.VatType"%>
<%@page import="java.util.List"%>
<%@page import="org.zizi.manager.LoginManager"%>
<%@page import="org.zizi.modal.User"%>

<%
	//登陆用户权限检查
	User u = null;
	if (session != null) u = Managers.get(LoginManager.class).getUser(session.getId());
	
	List<VatType> types = null;
	if (u == null)
	{
		response.sendRedirect("/login.jsp");
	}
	else
	{
		VatTypeDao dao = Managers.get(DaoManager.class).getDao(VatTypeDao.class);
	
		if (u.isMaintainType())
		{
			types = dao.listAllVatTypes(null);
			for (VatType vt : types) vt.setReferenced(dao.getVatTypeRefrencedCount(vt.getId()));
		}
	}
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>专用设置和技术维护费抵减系统</title>
<script type=""></script>
<jsp:include page="engine.jsp" ></jsp:include>
<script type="text/javascript">

function jump(url)
{
	location.href = url;
}

function logout()
{
	var action = new Action("Logout");
	var callback = function(result)
	{
		location.href = "/login.jsp";
	}
	Rpc.call(action, callback);
}

function addVatType()
{
	var n = $("name").value;
	if (n.length == 0)
	{
		alert("请输入项目类型名称！")
		return;
	}
	
	var action = new Action("AddVatType");
	action.name = n;
	var callback = function(result)
	{
		location.reload();
	}
	Rpc.call(action, callback);
}

function editVatType(obj, type)
{
	var action = new Action("EditVatType");
	action.id = obj.id;
	action.name = obj.name;
	action.sepMaxMoney = obj.sepMaxMoney;
	action.norMaxMoney = obj.norMaxMoney;
	
	if (type == 1)
	{
		var value = window.prompt("请编辑项目类型名称：", obj.name);
		if (value == null) return;
		action.name = value;
	}
	else if (type == 2)
	{
		var value = window.prompt("请编辑增值税发票限额：", obj.sepMaxMoney);
		if (value == null) return;
		if (value.length == 0) action.sepMaxMoney = null;
		else action.sepMaxMoney = parseFloat(value);
	}
	else if (type == 3)
	{
		var value = window.prompt("请编辑普通发票限额：", obj.norMaxMoney);
		if (value == null) return;
		if (value.length == 0) action.norMaxMoney = null;
		else action.norMaxMoney = parseFloat(value);
	}
	
	if (isNaN(action.norMaxMoney) || isNaN(action.sepMaxMoney))
	{
		alert("金额填写错误！");
		return;
	}

	var callback = function(result)
	{
		location.reload();
	}
	Rpc.call(action, callback);
}

function deleteVatType(id)
{
	if (!window.confirm("确定删除吗？")) return;
	
	var action = new Action("DeleteVatType");
	action.id = id;
	var callback = function(result)
	{
		location.reload();
	}
	Rpc.call(action, callback);
}

function enableVatType(id)
{
	var action = new Action("EnableVatType");
	action.id = id;
	var callback = function(result)
	{
		location.reload();
	}
	Rpc.call(action, callback);
}

</script>
<link href="css/common.css" rel="stylesheet" type="text/css" />
<style type="text/css">
.content .item_table {margin-top:15px; border-collapse:collapse; width:800px; font-size:12px;}
.content .item_table td {border:solid 1px #CCC;}
.content .item_table td a {text-decoration:none; color:#000;}
.content .item_table td a:hover {color:#F00;}
.content .item_table th {border:solid 1px #CCC;}
.content .add {width:800px; margin:15px auto; text-align:left;}
.nop {font-size:14px; font-weight:bold; padding-top:40px;}
.nop a {text-decoration:none; color:#00F;}
.content .item_table td a:hover {color:#F00;}
</style>
</head>
<body onload="resize()">
	<div id="container">
		<div class="top"></div>
		<div class="nav">
			<div class="navbuttons">
				<div class="navitem" style="background:url(images/main/submit_n.gif)" onclick="jump('/submit.jsp')"></div>
				<div class="navitem" style="background:url(images/main/history_n.gif)" onclick="jump('/history.jsp')"></div>
				<div class="navitem" style="background:url(images/main/types_a.gif)" onclick="jump('/types.jsp')"></div>
				<div class="navitem" style="background:url(images/main/empower_n.gif)" onclick="jump('/empower.jsp')"></div>
			</div>
			<div class="navcmds">
				<div style="background:url(images/main/logout.gif) no-repeat;"><a href="#" onclick="logout(); return false;">退出系统</a></div>
			</div>
		</div>
		<div class="content">
		<%
			if (u != null && u.isMaintainType()) {
		%>
			<table class="item_table" align="center" cellpadding="0" cellspacing="0" border="0">
				<tr>
					<th width="420">项目类型</th>
					<th width="100">增值税发票限额</th>
					<th width="100">普通发票限额</th>
					<th width="90">状态</th>
					<th width="90">引用次数</th>
					<th width="150">操作</th>
				</tr>
				<%
					if (types != null)
					{
						for (VatType t : types)
						{
							String obj = "{id:" + t.getId() + ",name:'" + t.getName() + "',sepMaxMoney:" + t.getSepMaxMoney() + ",norMaxMoney:" + t.getNorMaxMoney() + "}";
							
							out.println("<tr>");
							out.println("<td>" + t.getName() + "</td>");
							out.println("<td><a href=\"#\" onclick=\"editVatType(" + obj + ", 2); return false;\">" + (t.getSepMaxMoney() == null ? "-" : t.getSepMaxMoney()) + "</a></td>");
							out.println("<td><a href=\"#\" onclick=\"editVatType(" + obj + ", 3); return false;\">" + (t.getNorMaxMoney() == null ? "-" : t.getNorMaxMoney()) + "</a></td>");
							out.println("<td>" + (t.isEnabled() ? "√" : "<禁用>") + "</td>");
							out.println("<td>" + t.getReferenced() + "</td>");
							out.println("<td><a href=\"#\" onclick=\"editVatType(" + obj + ", 1); return false;\">[编辑]</a><a href=\"#\" onclick=\"deleteVatType(" + t.getId() + "); return false;\">[删除]</a><a href=\"#\" onclick=\"enableVatType(" + t.getId() + "); return false;\">" + (t.isEnabled() ? "[禁用]" : "[起用]") + "</a></td>");
							out.println("</tr>");
						}
					}
				%>
			</table>
			<div class="add">
				<input id="name" type="text" style="width:300px; margin-right:6px" /><input type="button" value="新建项目类型" onclick="addVatType()" />
			</div>
		<%
			} else {
		%>
			<div class="nop">对不起，您没有权限维护项目内容。<br/>若权限已更改，请<a href="#" onclick="logout(); return false;">重新登陆！</a></div>
		<%
			}
		%>
		</div>
	</div>
</body>
</html>